package com.littleken.foodieApi.controller;

import com.littleken.foodieCommon.Response.ErrorCode;
import com.littleken.foodieCommon.Response.ServerResponse;
import com.littleken.foodieCommon.Utils.BeanCopierUtil;
import com.littleken.foodiePojo.bo.AddressBO;
import com.littleken.foodiePojo.domain.UserAddressDO;
import com.littleken.foodieService.AddressService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/*
 * Create by littleken on 2020/11/25 9:52
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Resource
    private AddressService addressService;

    @GetMapping("/list/{userId}")
    public ServerResponse<List<UserAddressDO>> list(@PathVariable String userId) {
        List<UserAddressDO> userAllAddress = addressService.getUserAllAddress(userId);
        return ServerResponse.success(userAllAddress);
    }

    @PostMapping("/add")
    public ServerResponse<String> add(@RequestBody AddressBO addressBO) {
        UserAddressDO userAddressDO = BeanCopierUtil.copy(addressBO, UserAddressDO.class);
        String addressId = addressService.saveOrUpdateUserAddress(userAddressDO);
        return StringUtils.isBlank(addressId) ?
                ServerResponse.success(addressId) :
                ServerResponse.fail(ErrorCode.SQL_EXECUTE_ERR);
    }

    @PostMapping("/update")
    public ServerResponse<String> update(@RequestBody AddressBO addressBO) {
        if (StringUtils.isBlank(addressBO.getId())) {
            return ServerResponse.fail(ErrorCode.PARAMS_INVALID);
        }
        UserAddressDO userAddressDO = BeanCopierUtil.copy(addressBO, UserAddressDO.class);
        String addressId = addressService.saveOrUpdateUserAddress(userAddressDO);
        return StringUtils.isBlank(addressId) ?
                ServerResponse.success(addressId) :
                ServerResponse.fail(ErrorCode.SQL_EXECUTE_ERR);
    }

    @PostMapping("/delete")
    public ServerResponse delete(@RequestBody AddressBO addressBO) {
        if (StringUtils.isBlank(addressBO.getId())) {
            return ServerResponse.fail(ErrorCode.PARAMS_INVALID);
        }
        return addressService.deleteUserAddress(addressBO.getId());
    }

    /**
     * 设置为默认地址
     *
     * @param addressBO
     * @return
     */
    @PostMapping("/setDefalut")
    public ServerResponse setDefalut(@RequestBody AddressBO addressBO) {
        if (StringUtils.isBlank(addressBO.getId()) || StringUtils.isBlank(addressBO.getUserId())){
            return ServerResponse.fail(ErrorCode.PARAMS_INVALID);
        }
        return addressService.setAddressDefault(addressBO.getUserId(), addressBO.getId());
    }
}
